﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Linq;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("Values")]
	public partial class MultSiteDicCheckBoxList : Hxsoft.AppV1.UserControl
	{
		private LoadedValue<int[]> loadedSiteIds;
		public int[] SiteIds
		{
			get
			{
				if (loadedSiteIds.Loader == null)
					loadedSiteIds.Loader = () => (int[])ViewState["SiteIds"];
				return loadedSiteIds.Value;
			}
			set
			{
				if (loadedSiteIds.Setter == null)
					loadedSiteIds.Setter = v => ViewState["SiteIds"] = v;
				loadedSiteIds.Value = value;
			}
		}

		public string StringSiteIds
		{
			get
			{
				return Sites.BuildOwnedSiteIds(SiteIds);
			}
			set
			{
				SiteIds = Sites.DecompoundOwnedSiteIds(value);
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v => ViewState["ClassCode"] = v;
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicValueType>(ViewState["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
			set
			{
				if (loadedValueType.Setter == null)
					loadedValueType.Setter = v => ViewState["ValueType"] = v;
				loadedValueType.Value = value;
			}
		}

		private LoadedValue<RepeatDirection> loadedRepeatDirection;
		public RepeatDirection RepeatDirection
		{
			get
			{
				if (loadedRepeatDirection.Loader == null)
					loadedRepeatDirection.Loader = () => EnumExtensions.ConvertFromOrDefault<RepeatDirection>(ViewState["RepeatDirection"], RepeatDirection.Vertical);
				return loadedRepeatDirection.Value;
			}
			set
			{
				if (loadedRepeatDirection.Setter == null)
					loadedRepeatDirection.Setter = v => ViewState["RepeatDirection"] = v;
				loadedRepeatDirection.Value = value;
			}
		}

		private LoadedValue<int> loadedRepeatColumns;
		public int RepeatColumns
		{
			get
			{
				if (loadedRepeatColumns.Loader == null)
					loadedRepeatColumns.Loader = () => IntExtensions.ConvertFromOrDefault(ViewState["RepeatColumns"], 0);
				return loadedRepeatColumns.Value;
			}
			set
			{
				if (loadedRepeatColumns.Setter == null)
					loadedRepeatColumns.Setter = v => ViewState["RepeatColumns"] = v;
				loadedRepeatColumns.Value = value;
			}
		}

		private LoadedValue<RepeatLayout> loadedRepeatLayout;
		public RepeatLayout RepeatLayout
		{
			get
			{
				if (loadedRepeatLayout.Loader == null)
					loadedRepeatLayout.Loader = () => EnumExtensions.ConvertFromOrDefault<RepeatLayout>(ViewState["RepeatLayout"], RepeatLayout.Table);
				return loadedRepeatLayout.Value;
			}
			set
			{
				if (loadedRepeatLayout.Setter == null)
					loadedRepeatLayout.Setter = v => ViewState["RepeatLayout"] = v;
				loadedRepeatLayout.Value = value;
			}
		}

		public string[] Values
		{
			get
			{
				List<string> values = new List<string>();
				foreach (RepeaterItem siteItem in CtrSiteList.Items)
				{
					CheckBoxList CtrList = siteItem.FindControl("CtrList") as CheckBoxList;
					if (CtrList != null)
					{
						foreach (ListItem item in CtrList.Items)
							if (item.Selected) values.Add(item.Value);
					}
				}
				return values.ToArray();
			}
			set
			{
				EnsureBindData();
				foreach (RepeaterItem siteItem in CtrSiteList.Items)
				{
					CheckBoxList CtrList = siteItem.FindControl("CtrList") as CheckBoxList;
					if (CtrList != null)
					{
						foreach (ListItem item in CtrList.Items)
							item.Selected = value.Contains(item.Value);
					}
				}
			}
		}

		private LoadedValue<bool> loadedIsBindData;
		public bool IsBindData
		{
			get
			{
				if (loadedIsBindData.Loader == null)
					loadedIsBindData.Loader = () => BooleanExtensions.ConvertFromOrDefault(ViewState["IsBindData"], false);
				return loadedIsBindData.Value;
			}
			set
			{
				if (loadedIsBindData.Setter == null)
					loadedIsBindData.Setter = v => ViewState["IsBindData"] = v;
				loadedIsBindData.Value = value;
			}
		}

		protected void EnsureBindData()
		{
			if (IsBindData) return;

			this.DataBind();

			IsBindData = true;
		}

		public string[] values;
		protected void CtrSiteList_DataBinding(object sender, EventArgs e)
		{
			ValidateInitData();
			values = Values;
			if (SiteIds == null || SiteIds.Length == 0)
				CtrSiteList.DataSource = Sites.GetItems();
			else
				CtrSiteList.DataSource = Sites.GetItems(SiteIds);

			IsBindData = true;
		}

		protected void CtrSiteList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			CheckBoxList CtrList = e.Item.FindControl("CtrList") as CheckBoxList;
			Hxsoft.AppV1.Module.Site site = e.Item.DataItem as Hxsoft.AppV1.Module.Site;
			if (CtrList != null && site != null)
			{
				int? classId = DicClasses.GetIdentifier(site.SiteId, ClassCode);
				if (classId.HasValue)
				{
					CtrList.RepeatDirection = RepeatDirection;
					CtrList.RepeatColumns = RepeatColumns;
					CtrList.RepeatLayout = RepeatLayout;

					CtrList.DataSource = Dictionaries.GetItems(classId.Value, null);
					CtrList.DataTextField = "Name";
					CtrList.DataValueField = ValueType == DicValueType.Value ? "Value" : "DicId";
					CtrList.DataBind();

					if(values != null && values.Length > 0)
					{
						foreach (ListItem item in CtrList.Items)
						{
							item.Selected = values.Contains(item.Value);
						}
					}
				}
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.ApplicationPage.RegisterHeadIncludeFile("/_Client/Controls/MultSiteDicCheckBoxList/C.css", IncludeFileType.CSS);
			
			base.OnPreRender(e);
		}

		protected void ValidateInitData()
		{
			if (!loadedSiteIds.IsSetted)
				throw new DataException(string.Format(Res.ValidateBindingData_NotInit, "SiteIds"));

			if (!loadedClassCode.IsSetted)
				throw new DataException(string.Format(Res.ValidateBindingData_NotInit, "ClassCode"));
		}
	}
}